#light
open System.IO
open System.Threading

// This example is NOT working; can't figure out why not.

let readStream (stream: Stream) =
    async { use reader = new StreamReader(stream)
            let text = reader.ReadToEnd()
            do! Async.CancelCheck()
            return text }
            
let runAsync =
    use stream = new MemoryStream()
    use writer = new StreamWriter(stream, AutoFlush=true)
    writer.Write("NOT cancelled")
    stream.Position <- int64 0
    try
        let result = Async.Run(readStream (stream :> Stream))
        Async.CancelDefaultGroup()
        result
    with
    | :? System.OperationCanceledException -> "cancelled"

printfn "%A" runAsync